package com.pbd.client;

import java.util.ArrayList;
import java.util.List;

import sun.rmi.runtime.NewThreadAction;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.pbd.shared.ALBUMMASTER;
import com.pbd.shared.PARTY;

public class AlbumCreation extends Composite {
	private TextBox tbAlbumCode;
	private TextBox tbAlbumTitle;
	private TextArea taAlbumDescription;
	private TextBox tbAlbumIndex;
	private Long albumId;
	private Long partyId;
	private PbdListBox pbdListBox;
	private VerticalPanel vpMain;
	private VerticalPanel vpAlbum;
	// private RichTextArea richTextArea;
	// private RichTextToolbar richTextToolbar;
	private ALBUMMASTER albumMaster;

	public AlbumCreation(Long partyId) {
		this.partyId = partyId;
		initUI();
	}

	public AlbumCreation(Long partyId, Long albumId) {
		this.partyId = partyId;
		this.albumId = albumId;
		initUI();
	}

	public void initUI() {
		vpMain = new VerticalPanel();
		tbAlbumCode = new TextBox();
		tbAlbumCode.setWidth("250px");
		tbAlbumTitle = new TextBox();
		tbAlbumTitle.setWidth("250px");
		taAlbumDescription = new TextArea();
		taAlbumDescription.setWidth("250px");
		tbAlbumIndex = new TextBox();
		tbAlbumIndex.setWidth("250px");
		pbdListBox = new PbdListBox("250px");

		FlexTable basicFlex = new FlexTable();
		addBasicInfo(basicFlex);
		vpMain.add(basicFlex);

		HorizontalPanel hpButton = new HorizontalPanel();
		Button btnSave = new Button("Save");
		Button btnNew = new Button("New Album");
		hpButton.add(btnSave);
		hpButton.add(btnNew);
		hpButton.setSpacing(5);
		btnSave.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (albumMaster == null)
					albumMaster = new ALBUMMASTER();
				if (tbAlbumCode.getText() == null) {
					Window.alert("Enter the Album Code");
					return;
				}else
					albumMaster.setAlbumCode(tbAlbumCode.getText());
				
				if (tbAlbumTitle.getText() == null) {
					Window.alert("Enter the Album Title");
					return;
				}else
					albumMaster.setAlbumTitle(tbAlbumTitle.getText());

				if (tbAlbumIndex.getText() == null) {
					Window.alert("Enter the Album Index");
					return;
				}else
					albumMaster.setAlbumIndex(Integer.valueOf(tbAlbumIndex.getValue()));
				
				if(pbdListBox.getValue() == null || pbdListBox.getValue().equalsIgnoreCase("---Select---")){
					Window.alert("select the Layout");
					return;
				}else
					albumMaster.setLayout(pbdListBox.getValue());
				
				PARTY party = new PARTY();
				party.setPartyId(partyId);
				
				albumMaster.setParty(party);
				
				int widgetCount = vpAlbum.getWidgetCount();
				RichTextArea richTextArea = null;
				List mediaRelationList = null;
				
				if(widgetCount == 1){
					PBDFileUpload pbdFileUpload = (PBDFileUpload)vpAlbum.getWidget(0);
					mediaRelationList = pbdFileUpload.getAlbumMediaRelationList();
				}else if(widgetCount == 2){
					richTextArea = (RichTextArea)vpAlbum.getWidget(1);
					mediaRelationList = new ArrayList();
					ArrayList dataValueList = new ArrayList();
					dataValueList.add("");
					dataValueList.add("");
					dataValueList.add("");
					dataValueList.add(true);
					mediaRelationList.add(dataValueList);
				}else if(widgetCount == 3){
					richTextArea = (RichTextArea)vpAlbum.getWidget(2);
					PBDFileUpload pbdFileUpload = (PBDFileUpload)vpAlbum.getWidget(0);
					mediaRelationList = pbdFileUpload.getAlbumMediaRelationList();
				}
				
				String htmlContent = richTextArea == null ? null: richTextArea.getHTML();
				ServiceUtil.getMemberServiceAsync().saveAlbum(albumMaster, mediaRelationList, htmlContent, new AsyncCallback<List>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(List result) {
						String value = (String)result.get(0);
						if(value.equalsIgnoreCase("Success")){
							reset();
							Window.alert("Album saved successfully");
						}else{
							Window.alert(value);
						}
						
					}
				});
				
			}
		});
		btnNew.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				reset();	
			}
		});
		vpMain.add(hpButton);
		initWidget(vpMain);
	}

	private void addBasicInfo(FlexTable flexTable) {
		flexTable.setWidget(0, 0, new Label("Album Code"));
		flexTable.setWidget(1, 0, new Label("Album Title"));
		flexTable.setWidget(2, 0, new Label("Album Description"));
		flexTable.setWidget(3, 0, new Label("Album Index"));
		flexTable.setWidget(4, 0, new Label("Layout"));

		flexTable.getFlexCellFormatter().setWidth(0, 0, "200px");

		flexTable.setWidget(0, 1, tbAlbumCode);
		flexTable.setWidget(1, 1, tbAlbumTitle);
		flexTable.setWidget(2, 1, taAlbumDescription);
		flexTable.setWidget(3, 1, tbAlbumIndex);
		flexTable.setWidget(4, 1, pbdListBox);

		vpAlbum = new VerticalPanel();

		flexTable.setWidget(5, 1, vpAlbum);
		List<KeyValuePair> keyValueList = new ArrayList<KeyValuePair>();
		keyValueList.add(new KeyValuePair(-1L, "---Select---"));
		keyValueList.add(new KeyValuePair(1L, "Single Photo"));
		keyValueList.add(new KeyValuePair(2L, "Multiple Photo"));
		keyValueList.add(new KeyValuePair(3L, "Description"));
		keyValueList.add(new KeyValuePair(4L, "VPD"));
		keyValueList.add(new KeyValuePair(5L, "HPD"));
		keyValueList.add(new KeyValuePair(6L, "HPD*HP"));
		pbdListBox.setValueList(keyValueList);

		pbdListBox.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				String selectedValue = pbdListBox.getValue();
				vpAlbum.clear();
				if (!selectedValue.equalsIgnoreCase("---Select---")) {

					if (selectedValue.equalsIgnoreCase("Description")) {
						RichTextArea richTextArea = new RichTextArea();
						richTextArea.ensureDebugId("cwRichText-area");
						richTextArea.setSize("100%", "14em");
						RichTextToolbar richTextToolbar = new RichTextToolbar(richTextArea);
						richTextToolbar.ensureDebugId("cwRichText-toolbar");
						richTextToolbar.setWidth("100%");

						vpAlbum.add(richTextToolbar);
						vpAlbum.add(richTextArea);
					}

					else if (selectedValue.equalsIgnoreCase("VPD") || selectedValue.equalsIgnoreCase("HPD") || selectedValue.equalsIgnoreCase("HPD*HP")) {

						PBDFileUpload pbdFileUpload = new PBDFileUpload(partyId, "Album");
						RichTextArea richTextArea = new RichTextArea();
						richTextArea.ensureDebugId("cwRichText-area");
						richTextArea.setSize("100%", "14em");
						RichTextToolbar richTextToolbar = new RichTextToolbar(richTextArea);
						richTextToolbar.ensureDebugId("cwRichText-toolbar");
						richTextToolbar.setWidth("100%");
						vpAlbum.add(pbdFileUpload);
						vpAlbum.add(richTextToolbar);
						vpAlbum.add(richTextArea);
					} else {
						PBDFileUpload pbdFileUpload = new PBDFileUpload(partyId, "Album");
						vpAlbum.add(pbdFileUpload);

					}

				}

			}
		});

	}
	
	private void reset(){
		
		tbAlbumCode.setValue("");
		tbAlbumTitle.setValue("");
		taAlbumDescription.setValue("");
		tbAlbumIndex.setValue("");
		vpAlbum.clear();
		pbdListBox.setValue("---Select---");
	}

}
